package com.gitee.springioc.service.impl;

import com.gitee.springioc.service.OrderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;

import java.lang.reflect.Field;

/**
 * @author ming.yan-1
 * @version V1.0
 * @Title com.gitee.springioc.service.impl
 * @Description
 * @date 2018/9/14  上午11:18
 */
public class OrderBeanPostProcessor implements BeanPostProcessor {

    public static final Log logger = LogFactory.getLog(OrderBeanPostProcessor.class);

    @Nullable
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        logger.info("into orderBeanPostProcessor postProcessBeforeInitialization method beanName:"+beanName);
        if(bean instanceof OrderService){
            OrderService orderService = (OrderService)bean;
            orderService.print();
        }
        return bean;
    }

    @Nullable
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        logger.info("into orderBeanPostProcessor postProcessAfterInitialization method beanName:"+beanName);
        if(bean instanceof OrderService){
            OrderService orderService = (OrderService)bean;
            orderService.print();
            Class c = bean.getClass();
            c.getAnnotations();
            Field[] fields = c.getFields();
            for(Field f:fields){
                //字段的声明类型
                f.getType();
            }
        }
        return bean;
    }
}
